import random

from Systems.SystemsLogger import l

from Systems.Stars.StarTypes import *
from Systems.Solar import SolarBody
from Systems.Solar import SolarSystem
from Systems.Naming import *

from Systems.Stars.SolarCores import SolarCore

def makeRandomSystem():
    
    ss = SolarSystem()
    ss.name = makeRandomSystemName()
    
    cf = getRandomStarFactory()
    l.debug("star factory is " + str(cf))
    
    core = cf.makeCore()
    ss.setCore(core)

    slots = cf.getOrbitalTable(core)
    
    for slot in slots:
        ss.addBody(slot, cf.makeRandomBody(slot, core.getSolarMasses()))
        
    return ss

def getRandomStarFactory():
    r = random.randint(1,100)
    
    if r <= 3:
        return getRandomRareStar()
    elif r < 6:
        return YellowWhiteFactory()
    elif r < 16:
        return YellowFactory()
    elif r < 29:
        return OrangeFactory()
    elif r >= 29:
        return RedFactory()
   
def getRandomRareStar():
    r = random.randint(1,100)
    
    if r <= 1:
        return BlueFactory()
    elif r < 3:
        return BrownDwarfFactory()
    elif r < 4:
        return WhiteDwarfFactory()
    elif r < 6:
        return PulsarFactory()
    elif r < 8:
        return MagnetarFactory() 
    elif r < 10:
        return BlackFactory()
    elif r >= 10:
        return BlueWhiteFactory()    

def makeRandomSystemName():
    
    name = ""
    flip = random.randint(1, 2)
    if (flip == 1):
        l = random.randint(0, len(greekAlphabet) - 1)
        if(l < len(greekAlphabet)):
            name = greekAlphabet[l].title()
    else:
        l =  random.randint(0, len(marcomannicAlphabet) - 1)
        if(l < len(marcomannicAlphabet)):
             name = marcomannicAlphabet[l].title()
        
    flip =  random.randint(1, 5)
    if(flip ==1):
         l = random.randint(0, len(greekAlphabet) -1)
         name =name + " " + greekAlphabet[l].title()
    elif(flip == 2):
        l = random.randint(0, len(marcomannicAlphabet) -1)
        name =name + " " + marcomannicAlphabet[l].title()  
    
    flip = random.randint(1, 4)
    if(flip == 1):
         l = random.randint(0, len(randomPrefixes)- 1)
         name = name + " "  + randomPrefixes[l]
    
    l = random.randint(0, len(randomNames) - 1)
    name = name + " "  + randomNames[l].title()
    
    flip = random.randint(1, 4)
    if(flip == 1):
         l = random.randint(0, len(randomProfixes)- 1)
         name = name + " "  + randomProfixes[l]
    
    return name
    